﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace FGrubert.VpcManager.Objects.MachineComparer
{
    /// <summary>
    /// Comparer that compares machines by the number of nics
    /// </summary>
    public class NicComparer : IComparer<Machine>, IComparer
    {
        private SortOrder sortOrder;

        /// <summary>
        /// Creates a new comparer with the given sort order
        /// </summary>
        /// <param name="sortOrder">The sort order to use</param>
        public NicComparer(SortOrder sortOrder)
        {
            this.sortOrder = sortOrder;
        }

        #region IComparer<Machine> Member

        /// <summary>
        /// Compares two machines by the number of nics
        /// </summary>
        /// <param name="x">The first machine</param>
        /// <param name="y">The second machine</param>
        public int Compare(Machine x, Machine y)
        {
            if (sortOrder == SortOrder.Descending)
                return 0 - x.Nics.Count.CompareTo(y.Nics.Count);
            else
                return x.Nics.Count.CompareTo(y.Nics.Count);
            
        }

        #endregion


        #region IComparer Member

        /// <summary>
        /// Compares two items
        /// </summary>
        /// <param name="x">Either a list view item or a machine</param>
        /// <param name="y">Either a list view item or a machine</param>
        public int Compare(object x, object y)
        {
            if (x is ListViewItem && y is ListViewItem)
            {
                return this.Compare(((ListViewItem)x).Tag, ((ListViewItem)y).Tag);
            }
            else if (x is Machine && y is Machine)
            {
                return this.Compare((Machine)x, (Machine)y);
            }
            else
            {
                return 0;
            }
        }

        #endregion
    }
}
